;(function(){
'use strict';
/**
 * customer 吴尧
 * @ngdoc function
 * @name sasdApp.controller:customerAnalysisController
 * @description 物联网业务量统计分析
 * # customerAnalysisController
 * Controller of the sasApp
 */
angular.module('controllers.pbossSysBusiness.pbossSysBusinessStastic')
  .controller('pbossTradePercentageCtrl', pbossTradePercentageCtrl);

function pbossTradePercentageCtrl($scope,CoreService,sysBusinessStatisticFactory,tradePercentageService,$filter,$timeout,COREDATA,CoreLoading,BankAtaServer) {
  $scope.errorInfo = COREDATA.ERRORINFO;   //日期错误信息
  $scope.provinces = COREDATA.PROVINCES;

  $scope.initQuery = function(){
    sysBusinessStatisticFactory.queryBusinessName(function(res){
      $scope.shoumun = 1;
      $scope.businessList = res.data;
      $scope.queryData();
    });
  };
  $scope.translate ={
    searchTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1),
    businessCode:'BIP2B958'
  };

  var res = {};
  var searchTime;
  var pbossData;
  var option;
  var pbossEcharts = echarts.init(document.getElementById('pbossTradePercentage'));
  $scope.queryData = function(){
    var responsedData;
    var businessName;
    $scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
    $scope.send = {
      searchTime : CoreService.dateFormat($scope.translate.searchTime, 'yyyyMMdd'),
      businessCode : $scope.translate.businessCode
    };
    if($scope.shoumun === 0){
      CoreLoading.setState(true);
      searchTime = $filter('date')($scope.translate.searchTime,'yyyyMMdd');
      tradePercentageService.queryTradePercentage($scope.send,function(res){
        if(res.state === '10000' && res.data.length >= 1){
        angular.forEach($scope.businessList, function(item){
        if($scope.translate.businessCode === item.businessCode){
          businessName = item.businessName;
        }
      });
      var seriesData = [];
      angular.forEach(res.data, function(res,index) {
        seriesData[index] = {
          name:res.provinceName,
          value:res.volumeOf
        };
      });
      option = {
        title : {
          text: 'Pboss'+businessName+'业务成交量分省占比统计',
          x:'center'
        },
        animation: false,
        tooltip : {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}%'
        },
          series : [{
           name:'物联网',
           type:'pie',
           radius : '75%',
           center: ['50%', '55%'],
           data: seriesData
       }]
      };
      $timeout(function(){
        $scope.flagObj = true;
        CoreLoading.setState(false);
        pbossEcharts.clear();
        pbossEcharts.setOption(option);
      }, 1000);
      getPieData(option.series[0].data);
     }else if(res.data.length === 0){
            CoreService.dialog('查询无数据');
        }else {
            CoreService.dialog('查询错误');
        }
        CoreLoading.setState(false);
    });

  };



    function getPieData(allStData) {
      console.log(allStData);
      // var data = []
      angular.forEach(allStData, function(item,key) {
        var itemStyle = {};
        itemStyle.normal ={};
        itemStyle.normal.label = { show: false };
        itemStyle.normal.labelLine = { show: false,length:15};
        item.itemStyle = itemStyle;
        if(key < 8){
          item.itemStyle.normal.label.show = true;
          item.itemStyle.normal.labelLine.show = true;
          item.itemStyle.normal.label.formatter = function(param) {
                                    return param.name + '\n' + param.value + '%';
                                }
          allStData[key] = item;
        }else{
          allStData[key] = item;
        }
      });
      return allStData;
    }
  };
  $scope.queryData();
}
})();

